package hyj.qx.websocket.service.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class MessageRankService {
    private static final String MESSAGE_RANK_KEY = "chat:message_rank";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 增加用户消息计数
    public void incrementMessageCount(String userName) {
        redisTemplate.opsForZSet().incrementScore(MESSAGE_RANK_KEY, userName, 1);
    }

    // 获取Top10用户
    public Set<ZSetOperations.TypedTuple<String>> getTopUsers() {
        return redisTemplate.opsForZSet()
                .reverseRangeWithScores(MESSAGE_RANK_KEY, 0, 9);
    }
    public void clearRanking() {
        redisTemplate.opsForZSet().removeRange(MESSAGE_RANK_KEY, 0, -1);
    }
}
